﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;

namespace DAO
{
    public class DanhMucSanPhamDAO
    {
        public static DanhMucSanPhamDTO LayDanhMucSanPhamTheoID(int id)
        {
            MyDataBaseDataContext myDB = new MyDataBaseDataContext();
            DANH_MUC_SAN_PHAM danhMucSanPham = myDB.DANH_MUC_SAN_PHAMs.Single(c => c.ID == id);
            return new DanhMucSanPhamDTO(danhMucSanPham.ID, danhMucSanPham.MaNhomDanhMucSanPham,
                danhMucSanPham.TenDanhMucSanPham);
        }

        public static List<DanhMucSanPhamDTO> LayDanhSachDanhMucSanPhamTheoMaNhomDanhMucSanPham(int manhomdanhmucsanpham)
        {
            MyDataBaseDataContext myDB = new MyDataBaseDataContext();
            var listDanhMucSanPham = from danhmucsanpham in myDB.DANH_MUC_SAN_PHAMs
                                     where danhmucsanpham.MaNhomDanhMucSanPham == manhomdanhmucsanpham
                                     select danhmucsanpham;

            List<DanhMucSanPhamDTO> result = new List<DanhMucSanPhamDTO>();
            foreach (var item in listDanhMucSanPham)
            {
                DanhMucSanPhamDTO temp = new DanhMucSanPhamDTO(item.ID, item.MaNhomDanhMucSanPham, item.TenDanhMucSanPham);
                result.Add(temp);
            }
            return result;
        }

        public static bool ThemDanhMucSanPham(DanhMucSanPhamDTO danhmucsanphamDTO)
        {
            DANH_MUC_SAN_PHAM temp = new DANH_MUC_SAN_PHAM();
            temp.ID = danhmucsanphamDTO.ID;
            temp.MaNhomDanhMucSanPham = danhmucsanphamDTO.MaNhomDanhMucSanPham;
            temp.TenDanhMucSanPham = danhmucsanphamDTO.TenDanhMucSanPham;

            MyDataBaseDataContext myDB = new MyDataBaseDataContext();
            try
            {
                myDB.DANH_MUC_SAN_PHAMs.InsertOnSubmit(temp);
                myDB.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static bool XoaDanhMucSanPhamTheoID(int id)
        {
            MyDataBaseDataContext myDB = new MyDataBaseDataContext();
            DANH_MUC_SAN_PHAM temp = myDB.DANH_MUC_SAN_PHAMs.Single(c => c.ID == id);
            try
            {
                myDB.DANH_MUC_SAN_PHAMs.DeleteOnSubmit(temp);
                myDB.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}
